<template>
  <div class="login-container">
      <div class="login-box">
          <!-- 图像区域 -->
            <div class="avatar-box">
              <img src="../assets/heima.png" alt="">
            </div>
          <!-- 登陆表单区域 -->
            <el-form ref="form" :rules="rules" :model="form" class="login-form">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="form.username" prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="form.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
                </el-form-item>
                <!-- 按钮 -->
                <el-form-item class="btns">
                    <el-button @click="login" type="primary">登陆</el-button>
                    <el-button @click="resetForm" type="info">重置</el-button>
                </el-form-item>
            </el-form>
      </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单数据对象
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单校验规则
      rules: {
        // 用户名校验规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        // 密码校验规则
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {

  },

  methods: {
    // 点击重置
    resetForm () {
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.error('用户名或密码错误！')
        this.$message.success('登陆成功！')
        sessionStorage.setItem('token', res.data.token)
        // 跳转首页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.login-container {
    position: relative;
    width: 100%;
    height: 100%;
    background: #2b4b6b;
    .login-box {
        position: absolute;
        width: 450px;
        height: 300px;
        background: #fff;
        border-radius: 10px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        .avatar-box {
            width: 130px;
            height: 130px;
            border-radius: 50%;
            border: 1px solid #eee;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #fff;
            box-shadow: 0px 0px 10px green;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }
        }
    }
}
.login-form {
    position: absolute;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    bottom: 0;
    .btns {
        float: right;
    }
}
</style>
